package com.deductive.service.special.special_import.service;


import com.deductive.service.special.special_import.tool.AsyncTaskStatus;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class AsyncTaskManager {
    private final Map<String, AsyncTaskStatus> taskStatusMap = new ConcurrentHashMap<>();

    public AsyncTaskStatus createTask(String taskId, String taskType) {
        String encodedTaskId = encodeUrlParameter(taskId);
        AsyncTaskStatus status = new AsyncTaskStatus(encodedTaskId, taskType);
        taskStatusMap.put(encodedTaskId, status);
        return status;
    }

    private String encodeUrlParameter(String value) {
        try {
            return URLEncoder.encode(value, StandardCharsets.UTF_8.toString())
                    .replace("+", "%20")
                    .replace("%2F", "/")
                    .replace("%3A", ":");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Failed to encode URL parameter: " + value, e);
        }
    }

    public AsyncTaskStatus getTaskStatus(String taskId) {
        String encodedTaskId = encodeUrlParameter(taskId);
        return taskStatusMap.get(encodedTaskId);
    }

    public void updateTaskStatus(String taskId, String status, String message) {
        String encodedTaskId = encodeUrlParameter(taskId);
        AsyncTaskStatus taskStatus = taskStatusMap.get(encodedTaskId);
        if (taskStatus == null) {
            throw new IllegalArgumentException("Task not found: " + encodedTaskId);
        }

        String currentStatus = taskStatus.getStatus();
        switch (status) {
            case "READY":
                if (!"PENDING".equals(currentStatus)) {
                    throw new IllegalStateException("Task can only be set to READY from PENDING state");
                }
                taskStatus.setStatus("READY");
                taskStatus.setMessage(message);
                break;
            case "RUNNING":
                if (!"PENDING".equals(currentStatus)) {
                    throw new IllegalStateException("Task can only be set to RUNNING from READY state");
                }
                taskStatus.markAsRunning();
                break;
            case "COMPLETED":
                if (!"RUNNING".equals(currentStatus)) {
                    throw new IllegalStateException("Task can only be set to COMPLETED from RUNNING state");
                }
                taskStatus.markAsCompleted(message);
                break;
            case "FAILED":
                taskStatus.markAsFailed(message);
                break;
            default:
                throw new IllegalArgumentException("Invalid status: " + status);
        }
    }

    public void removeTask(String taskId) {
        taskStatusMap.remove(taskId);
    }
}